package com.backend.models;

import io.ebean.EbeanServer;
import io.ebean.ExpressionList;
import io.ebean.Finder;
import io.ebean.Model;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "sale_order")
@Data
public class Order extends Model {
    @Id
    public Integer id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "product_id")
    public Product product;
    @ManyToOne
    @JoinColumn(name = "delivery_address_id")
    public DeliveryAddress deliveryAddress;
    public Integer userId;
    public Integer salerId;
    public Date createTime;
    public Double expressFee = 0.00;
    public Double price = 0.00;
    public Boolean deleted = false;

    @Enumerated(EnumType.STRING)
    public ORDER_STATUS status = ORDER_STATUS.payed;

    @Enumerated(EnumType.STRING)
    public SALE_STATUS saleStatus = SALE_STATUS.payed;

    public enum ORDER_STATUS {
        payed("已付款"), sended("已发货"), finished("已完成");
        private String cname;

        public String getCname(){
            return this.cname;
        }

        ORDER_STATUS(String cname) {
            this.cname = cname;
        }
        public static ORDER_STATUS getObj(String name){
            for(ORDER_STATUS order_status : ORDER_STATUS.values()){
                if(order_status.name().equals(name)){
                    return order_status;
                }
            }
            return null;
        }
    }

    public enum SALE_STATUS {
        payed("买家已付款"), sended("已发货"), took_out("买家已确认收货"), finished("已售出");
        private String cname;

        SALE_STATUS(String cname) {
            this.cname = cname;
        }

        public String getCname(){
            return this.cname;
        }

        public static SALE_STATUS getObj(String name){
            for(SALE_STATUS sale_status : SALE_STATUS.values()){
                if(sale_status.name().equals(name)){
                    return sale_status;
                }
            }
            return null;
        }
    }

    public static Finder<Integer, Order> find = new Finder<>(Order.class);

    public static ExpressionList<Order> search() {
        return Order.find.query().where();
    }

    public static ExpressionList<Order> closeLazyLoading(){
        return Order.find.query().setDisableLazyLoading(true).where();
    }

}
